﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.ComponentModel;
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    public partial class FinPlusWebMenu : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        [State(Save = true)]
        public new double Height { get { return base.Height; } set { base.Height = value; } }
        [State(Save = true)]
        public new double Width { get { return base.Width; } set { base.Width = value; } }
        [State(Test = true)]
        public int ColumnsCount { get { return dataGrid.Columns.Count; } }
        [State(Test = true)]
        public int ItemsCount { get { return dataGrid.Items.Count; } }

        private enum DockControls { FinPlusTabPanel, Exception }
        private FinPlusTabPanel _tabPanel;
        private readonly FinPlusPages _pages;

        //construct
        public FinPlusWebMenu(IFinPlusComp tabPanel)
        {
            InitializeComponent();

            _pages = new FinPlusPages();
            _tabPanel = (FinPlusTabPanel)tabPanel;
            Load();
   
            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.Open: Open(); break;
                    case Cmds.Save: Save(); break;
                    case Cmds.Home: Home(); break;
                    case Cmds.Close: Close(); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = Level.Warning.Log(e.Message, e);
            }
        }

        public override void Dispose()
        {
            if(_pages != null)
                _pages.Close();

            if (_tabPanel != null)
                _tabPanel.Dispose();
        }

        //private
        private void Load()
        {
            try
            {
                _pages.Open(_tabPanel, Paths.Values["FolderViews"], Env);
                dataGrid.ItemsSource = _pages;
            }
            catch (Exception e)
            {
                Level.Error.Log(Id, Config, e.Message, e);
            }
        }

        private void Open()
        {
            var page = (FinPlusPage)dataGrid.SelectedItem;

            if (page.IsOpen)
                page.Close();
            else
                page.Open();
        }

        private void Save()
        {
            var page = (FinPlusPage)dataGrid.SelectedItem;

            if (page.IsOpen)
                page.Save();
        }

        private void Home()
        {
            var page = (FinPlusPage)dataGrid.SelectedItem;
            
            if (page.IsOpen)
                page.Home();
        }

        private void Close()
        {
            var page = (FinPlusPage)dataGrid.SelectedItem;

            if (page.IsOpen)
                page.Close();
        }

        //cmds
        private enum Cmds { Open, Save, Home, Close }
    }

    internal class FinPlusPages : ObservableCollection<FinPlusPage>
    {
        public void Open(FinPlusTabPanel tabPanel, string configsPath, Env env)
        {
            var dir = new DirectoryInfo(configsPath);
            foreach (var clientIOFile in dir.GetFiles("*.view"))
                this.Add(new FinPlusPage(tabPanel, clientIOFile, env));
        }

        public void Close()
        {
            try
            {
                foreach (var page in this)
                    page.Close();
            }
            catch (Exception e)
            {
                Level.Warning.Log(e.Message, e);
            }
        }
    }

    internal class FinPlusPage : INotifyPropertyChanged
    {
        public string Name { get; set; }
        public bool IsOpen { get { return _isOpen; } set { _isOpen = value; NotifyPropertyChanged("IsOpen"); } }
        public event PropertyChangedEventHandler PropertyChanged;

        private FinPlusTabPanel _tabPanel;
        private FileInfo _fileInfo;
        private Env _env;
        private string _configPath, _guid, _user;
        private bool _isOpen;

        //public
        public FinPlusPage(FinPlusTabPanel tabPanel, FileInfo fileInfo, Env env)
        {
            _tabPanel = tabPanel;
            _fileInfo = fileInfo;
            _env = env;
            _user = Environment.UserName;
            _configPath = fileInfo.FullName.Replace(".view", "");
            _guid = Guids.New();
            Name = fileInfo.Name.Split('.')[0];
        }

        public void Open()
        {
            _tabPanel.CmdRouter(Cmds.Open.ToString(), _configPath);
            _tabPanel.TabClosed += Tab_OnClosed;
            IsOpen = true;
        }

        public void Save()
        {
            _tabPanel.CmdRouter(Cmds.Save.ToString());
        }

        public void Home()
        {
            _tabPanel.CmdRouter(Cmds.Home.ToString());
        }

        public void Close()
        {
            _tabPanel.CmdRouter(Cmds.Close.ToString());
            IsOpen = false;
        }

        //private
        private void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        //cmds
        private enum Cmds { Open, Save, Home, Close, Closed, }

        //events
        private void Tab_OnClosed(object s, EventArgs e) 
        {
            if(s != null && s.ToString().Equals(Name))
                IsOpen = false;
        }
    }
}
